<template>
  <el-main class="el-mainContent height100 manage  bgColor "
  v-loading="loading"
    :element-loading-text="loadingText"
    element-loading-spinner="el-icon-loading"
    element-loading-background="rgba(0, 0, 0, 0.5)"
    >
    <div class="partContent">
      <div class="part part0 paddingT20" style=" border-top-left-radius: 0;border-top-right-radius: 0;">
        <el-row class="  setHeader paddingT0" > 
      <el-col :span="12"><p class="el-divider-header">{{$t('cache_manage')}}</p></el-col>
          <el-col :span="12" style="text-align:right">
           <button_item  :btnItemPlain="true" :btnItemLabel="$t('clearConfig')"  @onBtnItemClick="clearConfig" />
          </el-col>
        </el-row>
    <div class="tipCleanText">
      <i class="el-message-box__status el-icon-warning"></i>
      <span class="spanText ">{{$t('clean_cache')}}</span>
    </div> 
      </div>
      <div class="part divisionPart">
        <div>
      <el-row class="  setHeader" > 
      <el-col :span="12"> <p class="el-divider-header">{{$t('data_manage')}}</p></el-col>
          <el-col :span="12" style="text-align:right">
         <button_item  :btnItemPlain="true" :btnItemLabel="$t('clearConfigId')"  @onBtnItemClick="clearConfigId" v-if="!!customAppsId" />
         <button_item  :btnItemPlain="true" :btnItemLabel="$t('synchronization')"  @onBtnItemClick="synchronization" v-if="!!customAppsId" />
         <button_item  :btnItemPlain="true" :btnItemLabel="$t('indexOptimization')"  @onBtnItemClick="indexOptimization"  v-if="!!customAppsId"/>
        </el-col>
        </el-row>
    
    <el-form class="marginT24 marginL30  contentPadding0">
      <el-form-item :label="$t('select_soft')">
        <el-col :span="12">
          <el-select :placeholder="$t('placeholder_select')" v-model="customAppsId" size="small">
            <el-option
              :label="$t('all')"
              :value="allcustomAppsId">
            </el-option>
            <el-option
              v-for="item in customApps"
              :key="item.id"
              :label="item.name"
              :value="item.id">
            </el-option>
          </el-select>
        </el-col>
        
      </el-form-item>
    </el-form> 
    <div class="tipCleanText">
      <i class="el-message-box__status el-icon-warning"></i>
      <span class="spanText ">{{$t('clean_redundant')}}</span>
    </div> 
    </div>
      </div>
    </div>
  </el-main>
</template>

<script>
import {delMsg} from '../utils/elementui'

export default {
  data(){
    return {
      customApps:[],
      customAppsId:[],
      loading:false,
      loadingText:this.$t('msg_opt_progress'),
      allcustomAppsId: [],//所有的软件id
    }
  },
  computed:{
  },
  created() {
    this.$api.getCustomAllApps(
      {
        onSucess: resp => {
          let temp=resp.data.data.datas
          this.customApps = temp;
          for( let i = 0 ; i < temp.length; i ++) {
            this.allcustomAppsId[i] = temp[i].id
          }
          this.customAppsId=this.allcustomAppsId
        }
      }
    );
  },
  methods: {
    /**清除缓存数据 
     * */ 
    async clearConfig(){
      await delMsg(this.$t('msg_op'), this.$t('msg_tip'),this.$t('msg_confirm'),this.$t('cancel'))
      this.$api.clearConfig(
        {
          onSucess: resp => {
            if(resp.data.errcode==0){
              this.loading=true;
              this.loadingText=this.$t('msg_clear')
              this.getClearConfigState();
            }
            
          }
        }
      );
    },
    /**定期获取清除缓存数据 的状态
     * */ 
    getClearConfigState(){
      let timer = setInterval(()=>{
        this.$api.getClearConfigState(
          {
            onSucess: resp => {
              if(resp.data.data!='正在清除中'){
                this.loading=false;
                this.$message({
                  showClose: true,
                  message: this.$t('msg_clear_success'),
                  type: "success"
                });
                clearInterval(timer)
              }
            }
          }
        );
      },1000)
    },
   async clearConfigId(){
      if(this.customAppsId==""){
        this.$message({
          showClose: true,
          message: this.$t('msg_select_sofeware'),
          type: "error"
        });
        return false;
      }
      //选择所有
      if(JSON.stringify(this.customAppsId)===JSON.stringify(this.allcustomAppsId)){
        this.customAppsId = []
      }
      await delMsg(this.$t('msg_op'), this.$t('msg_tip'),this.$t('msg_confirm'),this.$t('cancel'))
      this.$api.clearConfigId(this.customAppsId,
        {
          onSucess: resp => {
            if(resp.data.errcode==0){
              if(resp.data.data.indexOf("成功")!=-1){
                this.$message({
                  showClose: true,
                  message: resp.data.data,
                  type: "success"
                });
              }else{
                this.$message({
                  showClose: true,
                  message: resp.data.data,
                  type: "error"
                });
              }
              
            }
            
          }
        }
      );
    },
    /**
     * 同步数据表单
     */
    async synchronization(){
      if(this.customAppsId==""){
        this.$message({
          showClose: true,
          message: this.$t('msg_select_sofeware'),
          type: "error"
        });
        return false;
      }
      await delMsg(this.$t('msg_op'), this.$t('msg_tip'),this.$t('msg_confirm'),this.$t('cancel'))
      this.$api.synchronization(this.customAppsId,
        {
          onSucess: resp => {
            if(resp.data.errcode==0){
              this.loading=true;
              this.loadingText=this.$t("msg_syn")
              this.getSynchronizationState();
            }else{
              this.$message({
                showClose: true,
                message: this.$t("msg_syn_failed"),
                type: "error"
              });
            }
            
          }
        }
      );
    },
    /**
     * 获取同步数据表单状态
     */
    getSynchronizationState(){
      let timer = setInterval(()=>{

        this.$api.getSynchronizationState(this.customAppsId,
          {
            onSucess: resp => {
              if(resp.data.data!='正在同步中'){
                this.loading=false;
                this.$message({
                  showClose: true,
                  message: this.$t("msg_syn_succeed"),
                  type: "success"
                });
                clearInterval(timer)
              }
              
            }
          }
        );
      },1000);
    },
    async indexOptimization(){
      if(this.customAppsId==""){
        this.$message({
          showClose: true,
          message: this.$t('msg_select_sofeware'),
          type: "error"
        });
        return false;
      }
      this.loading=true;
      this.loadingText=this.$t('msg_opt_progress')
      await delMsg(this.$t('msg_op'), this.$t('msg_tip'),this.$t('msg_confirm'),this.$t('cancel'))
      this.$api.indexOptimization(this.customAppsId,
        {
          onSucess: resp => {
            this.loading=false;
            if(resp.data.errcode==0){
              if(resp.data.errcode==0){
                this.$message({
                  showClose: true,
                  message: resp.data.data,
                  type: "success"
                });
              }else{
                this.$message({
                  showClose: true,
                  message: resp.data.data,
                  type: "error"
                });
              }
              
            }
            
          }
        }
      );
    }
  },
};
</script>
<style lang="scss" scoped>

</style>